//
//  ViewController.m
//  应用管理
//
//  Created by 刘剑秋 on 15/3/4.
//  Copyright (c) 2015年 Gantry's App Home. All rights reserved.
//

#import "ViewController.h"
#import "AppView.h"
#import "App.h"

#define appW 85
#define appH 90

@interface ViewController ()

@property (nonatomic, strong) NSArray * apps;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initApps];
    
    int marginW = (self.view.frame.size.width - 3*appH)/3;
    int marginH = 30;
    
    for (int i=0; i<self.apps.count; i++) {
        
        int col = i%3;
        int row = i/3;
        
        int appX = marginW + (appW+marginW)*col;
        int appY = marginH + (appH+marginH)*row;
        AppView * appView = [AppView appViewInitWithApp:_apps[i]];
        CGRect frame = appView.frame;
        frame.origin.x = appX;
        frame.origin.y = appY;
        appView.frame = frame;
        
        [self.view addSubview:appView];

    }
    
}

/**
 * 从plist中初始化apps数组
 */
- (void)initApps {
    
    if (_apps == nil) {
        NSString * path = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"];
        NSArray * dictArray = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray * appArray = [NSMutableArray array];
        for (NSDictionary * dict in dictArray) {
            App * app = [[App alloc] initWithDict:dict];
            [appArray addObject:app];
        }
        _apps = appArray;
    }
    NSLog(@"成功初始化apps数组，数组长度为%ld",[_apps count]);
}

@end
